using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imagination;
using System.Diagnostics;

namespace Imagination.Collisions
{
	public class Collision
	{
        private Thing       thing1;
        private Thing       thing2;
		private Vector3		relativeVelocity;
		private Vector3		collisionNormal;
		private Vector3		collisionTangent;
		private float		relativeVelocityN;
		private float		relativeVelocityT;
		private Vector3		collisionPoint;

        public Collision(Thing thing1, Thing thing2)
		{
			this.thing1 = thing1;
			this.thing2 = thing2;
		
			relativeVelocity = thing1.Velocity - thing2.Velocity;
						
			collisionNormal = thing1.Position - thing2.Position;
            if(collisionNormal.Length() == 0)
                collisionNormal = new Vector3(0, 0, 1);

            collisionNormal.Normalize();
						
			relativeVelocityN = Vector3.Dot(relativeVelocity, collisionNormal);
						
			collisionTangent = relativeVelocity - (relativeVelocityN * collisionNormal);

            if (collisionTangent.Length() == 0)
                collisionTangent = new Vector3(0, 1, 0);

            collisionTangent.Normalize();

            relativeVelocityT = Vector3.Dot(relativeVelocity, collisionTangent);
            Debug.Assert(!float.IsNaN(relativeVelocityT));


            collisionPoint = thing2.Position + (thing2.Boundary.Radius * collisionNormal);
			Vector3 pt1 = collisionPoint - thing1.Position;
			Vector3 pt2 = collisionPoint - thing2.Position;
        }
		
		public Thing Thing1
		{
			get { return thing1; }
		}
		
		public Thing Thing2
		{
			get { return thing2; }
		}
		
		public Vector3 RelativeVelocity
		{
			get { return relativeVelocity; }
		}
		
		public Vector3 CollisionNormal
		{
			get { return collisionNormal; }
		}
		
		public Vector3 CollisionTangent
		{
			get { return collisionTangent; }
		}
		
		public float RelativeVelocityN
		{
			get { return relativeVelocityN; }
		}
		
		public float RelativeVelocityT
		{
			get { return relativeVelocityT; }
		}
		
		public Vector3 CollisionPoint
		{
			get { return collisionPoint; }
		}
	}
}
